$(function() {
	// 进度条作用
	$(document).ajaxStart(function() {
		Pace.restart();
	});
	
	//日期控件
	$('.datepicker').datepicker({
		language : "zh-CN",
		autoclose : true,//选中之后自动隐藏日期选择框
		clearBtn : true,//清除按钮
		format : "yyyy-mm-dd"
	});

	table_tfoot_colspan_load();

	table_business_data_load();
	
	$("#search_tableName").change(function() {
		table_business_data_load();
	});
	
	$("#search_createTime").change(function() {
		table_business_data_load();
	});
	$("#search_btn").click(function() {
		table_business_data_load();
	});
	
	$("#checkall_btn").click(function() {
		checkAll();
	});
	
	// 重置
	$("#search_resetbtn").click(function() {
		resetInput();
		table_business_data_load();
	});
	
	//生成代码
	$("#generateCodeBtn").click(
		function() {
			var tableName_array = new Array();
			$("input[name='serialChk']:checked").each(
					function() {
						tableName_array.push("'"
								+ $(this).parent().next()
										.text() + "'");//向数组中添加元素  
					});
			if (tableName_array.length == 0) {
				alert("请至少选中一项");
				return;
			}
			
			location.href = "code?tables=["
					+ tableName_array + "]";
		}
	);
	
})

/*
初始化业务数据
pageType参数：   1 首页    2上一页   3下一页   4尾页 
*/
function table_business_data_load(currPageParam){
	//参数对象
	var param = new Object();
	/*分页参数*/
	currPageParam == null? 1:currPageParam;
	param.currPage = currPageParam;
	if (currPageParam == null) {
		param.currPage = 1;
	}
	//判断cookie中是否存在
	param.limit = getCookie("pageSizeSelByCookie");
	if(param.limit == null){
		param.limit = $("#pageSizeSel").val();
	}
	$("#pageSizeSel").find("option[value = '"+param.limit+"']").attr("selected","selected");
	
	/*查询条件参数*/
	param.tableName = $("#search_tableName").val();
	param.createTime = $("#search_createTime").val();
	
	/*请求后台*/
	$.ajax({
		url : "list",
		dataType : "JSON",
		data : param,
		success : function(data) {
			if(data.code == 0){
				//结果json串转成对象
				var pageObj = eval(data.page); // page对象
				var listObj = eval(data.page.list); // 数据集
				loadPage(pageObj);
				table_data_set(listObj);
			}else{
				//调用失败提示
				alertDivFadeSet(2,1000);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//调用失败提示
			alertDivFadeSet(2,1000,function(){
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
				alert(textStatus);
			});
		}
	})
	
}


/**
 * 给table填充数据
 * @param data_list  数据集
 */
function table_data_set(data_list){
	var html = null;
	$.each(data_list,function(i, result) {
		var serialNum = parseInt(i + 1); // 序号
		html += "<tr>";
			html += "<td style='text-align:center'><input name='serialChk' type='checkbox' style='vertical-align:middle; margin: -2px 0px 0px'/>&nbsp;" + serialNum + "</td>";
			html += "<td>"+result["tableName"]+"</td>"
			html += "<td>"+result["tableComment"]+"</td>"
			html += "<td>"+result["engine"]+"</td>"
			html += "<td>"+result["createTime"]+"</td>"
		html += "</tr>";
	})
	
	$(".box-body").find("tbody").html(html);
}